﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Futajma.MigrationStudio.LiveLinkImporter.Tdx
{
    public class Folder : Item
    {

        public Folder(XElement folderElement, TDX tdx)
            : base(folderElement, tdx)
        {
           
        }

        public List<Folder> SubFolders()
        {
            var folderList = new List<Folder>();

            var folders = base.ItemElement.Element("folders");

            if (folders != null)
            {
                foreach (var folder in folders.Elements("folder"))
                {
                    folderList.Add(new Folder(folder, TDX));
                }
            }
            return folderList;
        }

        public List<Item> GetItems()
        {
            var itemList = new List<Item>();
            var fileName = this.GetStringAttribute("itemsFile");

            if (fileName != null)
            {
                var docx = TDX.StdxDocuments[fileName];
                var href = GetStringAttribute("href");

                var items = docx.Root.Descendants("document").Where(x => (string)x.Attribute("folderRef") == href);

                foreach (var item in items)
                {
                    var newItem = new Item(item, TDX);
                    itemList.Add(newItem);
                }
            }
            return itemList;
        }
        
    }
}
